package com.example.thealarm;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyProvider extends ContentProvider {
    public static final String LOG_TAG = AlarmReminderHelper.class.getSimpleName();
    public static final int REMINDER = 100;
    public static final int REMINDER_ID =101;
    public static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        sUriMatcher.addURI(TheDataBase.CONTENT_AUTHORITY,TheDataBase.PATH_VEHICLE,REMINDER);
        sUriMatcher.addURI(TheDataBase.CONTENT_AUTHORITY,TheDataBase.PATH_VEHICLE+"/#",REMINDER);
    }
    private AlarmReminderHelper mDbHelper;
    public boolean onCreate() {
        mDbHelper = new AlarmReminderHelper(getContext());
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        SQLiteDatabase database = mDbHelper.getReadableDatabase();
        Cursor cursor = null;
        SQLiteQueryBuilder myQuery = new SQLiteQueryBuilder();
        myQuery.setTables(TheDataBase.DataBaseEntry.TABLE_NAME);
        Cursor cr = myQuery.query(database,null,selection,selectionArgs,null,null,null);
        cr.setNotificationUri(getContext().getContentResolver(),uri);
        return cr;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        final int match = sUriMatcher.match(uri);
        switch (match){
            case REMINDER:
                return TheDataBase.DataBaseEntry.CONTENT_LIST_TYPE;
            case REMINDER_ID:
            return TheDataBase.DataBaseEntry.CONTENT_ITEM_TYPE;
            default:
                throw  new IllegalStateException("未命名URI"+uri+"捕捉"+match);
        }
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        final int match = sUriMatcher.match(uri);
        switch (match){
            case REMINDER:
                return insertReminder(uri,contentValues);
            default:
                throw  new IllegalArgumentException("Here in insert where the uri is"+uri+"matches"+match);

    }}
    private Uri insertReminder(Uri uri,ContentValues values){
    SQLiteDatabase database = mDbHelper.getWritableDatabase();
    long id = database.insert(TheDataBase.DataBaseEntry.TABLE_NAME,null,values);
    if(id==-1){
        Log.e("Filled in the insert",uri+"");
        return null;
    }
    getContext().getContentResolver().notifyChange(uri,null);
    return ContentUris.withAppendedId(uri,id);
        }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
        SQLiteDatabase database = mDbHelper.getWritableDatabase();
        int rowDeleted = 0;
        final int match = sUriMatcher.match(uri);
        switch (match){
            case REMINDER:
                rowDeleted=database.delete(TheDataBase.DataBaseEntry.TABLE_NAME,s,strings);
                break;
            case REMINDER_ID:
                s=TheDataBase.DataBaseEntry._ID+" =?";
                strings = new String[]{String.valueOf(ContentUris.parseId(uri))};
                rowDeleted=database.delete(TheDataBase.DataBaseEntry.TABLE_NAME,s,strings);
                break;
            default:
                throw  new IllegalArgumentException("Here in deleted where the uri is"+uri+"matches"+match);
        }
        getContext().getContentResolver().notifyChange(uri,null);
        return rowDeleted;
    }
    private int updateReminder(Uri uri,ContentValues values,String selection,String[] selectionArgs){
        if(values.size()==0){
            return 0;
        }
        SQLiteDatabase database = mDbHelper.getWritableDatabase();
        int rowUpdated = database.update(TheDataBase.DataBaseEntry.TABLE_NAME,values,selection,selectionArgs);
        if(rowUpdated!=0)
            getContext().getContentResolver().notifyChange(uri,null);
        return rowUpdated;
    }
    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        final int match = sUriMatcher.match(uri);
        switch (match){
            case REMINDER:
                return updateReminder(uri,contentValues,s,strings);
            case REMINDER_ID:
                s=TheDataBase.DataBaseEntry._ID+" =?";
                strings = new String[]{String.valueOf(ContentUris.parseId(uri))};
                return updateReminder(uri,contentValues,s,strings);
            default:
                throw  new IllegalArgumentException("Here in updated where the uri is"+uri+"matches"+match);
        }
    }

}
